<template>
  <div class="building-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <div class="search-label">楼宇名称：</div>
      <el-input v-model="params.name" clearable placeholder="请输入内容" class="search-main" @change="doSearch" />
      <el-button type="primary" @click="doSearch">查询</el-button>
    </div>
    <!-- 添加楼宇弹框 -->
    <el-dialog
      title="添加楼宇"
      :visible="dialogVisible"
      width="580px"
      @close="closeDialog"
    >
      <!-- 表单接口 -->
      <div class="form-container">
        <el-form ref="addForm" :model="addForm" :rules="addFormRules">
          <el-form-item label="楼宇名称" prop="name">
            <el-input v-model="addForm.name" />
          </el-form-item>
          <el-form-item label="楼宇层数" prop="floors">
            <el-input v-model="addForm.floors" />
          </el-form-item>
          <el-form-item label="在管面积" prop="area">
            <el-input v-model="addForm.area" />
          </el-form-item>
          <el-form-item label="物业费" prop="propertyFeePrice">
            <el-input v-model="addForm.propertyFeePrice" />
          </el-form-item>
        </el-form>
      </div>

      <template #footer>
        <el-button size="mini" @click="dialogVisible = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="confirmAdd">确 定</el-button>
      </template>
    </el-dialog>

    <el-button type="primary" @click="addBuilding">添加楼宇</el-button>
    <!-- 表格区域 -->
    <div class="table">
      <el-table
        style="width: 100%"
        :data="tableList"
      >
        <el-table-column type="index" label="序号" width="300" />
        <el-table-column
          label="楼宇名称"
          width="220"
          prop="name"
        />
        <el-table-column
          label="层数"
          width="180"
          prop="floors"
        />
        <el-table-column
          label="在管面积(m²)"
          width="180"
          prop="area"
        />
        <el-table-column
          label="物业费(元/m²)"
          width="180"
          prop="propertyFeePrice"
        />
        <el-table-column
          label="状态"
          width="180"
          prop="status"
        ><template #default="scope">
          {{ formatStatus(scope.row.status) }}
        </template>
        </el-table-column>
        <el-table-column
          label="操作"
        >
          <template #default="scope">
            <el-button
              size="mini"
              type="text"
              @click="edit(scope.row.id)"
            >编辑</el-button>
            <el-button
              size="mini"
              type="text"
              @click="confirmDel(scope.row.id)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        layout="prev, pager, next"
        :total="total"
        @current-change="pageChange"
      />

    </div>
  </div>
</template>

<script>
import { getBuildingListAPI, createBuildingListAPI, delBuildingListAPI, editBuildingListAPI, gainBuildingListAPI } from '@/api/building'
export default {
  name: 'Building',
  data() {
    return {
      addForm: {
        name: '',
        floors: null,
        area: null,
        propertyFeePrice: null
      },
      addFormRules: {
        name: [
          { required: true, message: '请输入楼宇名称', trigger: 'blur' }
        ],
        floors: [
          { required: true, message: '请输入楼宇层数', trigger: 'blur' }
        ],
        area: [
          { required: true, message: '请输入楼宇面积', trigger: 'blur' }
        ],
        propertyFeePrice: [
          { required: true, message: '请输入楼宇物业费', trigger: 'blur' }
        ]
      },
      params: {
        page: 1, // 当前页数
        pageSize: 10, // 每页条数
        name: '' // 搜索的关键词 企业名称
      },
      tableList: [], // 列表
      status: 0,
      total: 0,
      dialogVisible: false
    }
  },
  computed: {
    id() {
      return this.addForm.id
    }
  },

  created() {
    this.getList()
  },
  methods: {
    async getList() {
      const res = await getBuildingListAPI(this.params)
      // console.log(res)
      this.tableList = res.data.rows
      this.total = res.data.total
      this.status = this.tableList.status
      // console.log(this.status)
    },
    // 准备查询后调方法
    doSearch() {
      this.params.page = 1
      this.getList()
    },
    formatStatus(status) {
      // const res = { 0: '租赁中', 1: '闲置中' }
      // return res[status]
      if (status === 0) {
        status = '租赁中'
      } else if (status === 1) {
        status = '闲置中'
      }
      return status
    },
    pageChange(page) {
      console.log(page)
      this.params.page = page
      this.getList()
    },
    // 编辑
    async edit(row) {
      this.dialogVisible = true
      // 2. 解构必要字段
      const res = await gainBuildingListAPI(row)
      console.log(res)
      const { id, area, floors, name, propertyFeePrice } = res.data
      this.addForm = {
        id, area, floors, name, propertyFeePrice
      }
    },
    addBuilding() {
      this.dialogVisible = true
    },
    closeDialog() {
      this.dialogVisible = false
      this.$refs.addForm.resetFields()
    },
    // 添加
    confirmAdd() {
      this.$refs.addForm.validate(async(valid) => {
        if (valid) {
          console.log('添加')
          // 判断是添加还是编辑
          if (this.addForm.id) {
            await editBuildingListAPI(this.addForm)
          } else {
            // 1. 提交接口
            await createBuildingListAPI(this.addForm)
          }
          // 2. 获取列表
          this.getList()
          // 3. 关闭弹框
          this.dialogVisible = false
        }
      })
    },
    // 删除
    confirmDel(id) {
      console.log(id)
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        await delBuildingListAPI(id)
        this.getList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.department-container{
  padding:10px;
}
.search-container{
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237,237,237,.9);;
  padding-bottom: 20px;
  .search-label{
    width:100px;
  }
  .search-main{
    width: 220px;
    margin-right: 10px;
  }
}
</style>
